package com.college.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.college.DTO.studentDTO.CourseInfoDTO;
import com.college.DTO.studentDTO.CreditDTO;
import com.college.DTO.studentDTO.InstituteInfoDTO;
import com.college.DTO.studentDTO.StudentMeetDTO;
import com.college.entity.*;
import com.college.mapper.StudentInfoMapper;
import com.college.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.college.utils.R;
import org.apache.ibatis.annotations.Lang;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 学生信息 服务实现类
 * </p>
 *
 * @author yoki
 * @since 2024-05-29
 */
@Service
public class StudentInfoServiceImpl extends ServiceImpl<StudentInfoMapper, StudentInfo> implements StudentInfoService {

    @Resource
    private SchoolCalendarInfoService calendarInfoService;
    @Resource
    private ClassInfoService classInfoService;
    @Resource
    private MajorInfoService majorInfoService;
    @Resource
    private DepartmentInfoService departmentInfoService;
    @Resource
    private CourseInfoService courseInfoService;
    @Resource
    private ScheduleInfoService scheduleInfoService;

    /**
     * 等于在校周数相加  根据学生的入学日期获取校历 再根据当前最新校历 记得
     * 获取学生的在校时间
     * @return
     */
    @Override
    public R<StudentMeetDTO> getMeetByNum(String num) {
        //根据编号 获取当前的用户信息
        StudentInfo studentInfo = getOne(new LambdaQueryWrapper<StudentInfo>().eq(StudentInfo::getStudentNumber, num));
        //获取入学信息
        Date registerTime = studentInfo.getEnrollmentTime();
        String s = registerTime.toString();
        //获取到入学的校历
        SchoolCalendarInfo begin = calendarInfoService.getOne(new LambdaQueryWrapper<SchoolCalendarInfo>()
                .eq(SchoolCalendarInfo::getBeginTime, registerTime));
        //获取第一学期的结束时间
        Date endTime1 = begin.getEndTime();
        //获取当前最新校历
        SchoolCalendarInfo last = calendarInfoService.getOne(new LambdaQueryWrapper<SchoolCalendarInfo>()
                .orderByDesc(SchoolCalendarInfo::getEndTime).last("limit 1"));
        Date endTime2 = last.getEndTime();
        List<SchoolCalendarInfo> list = calendarInfoService.list(new LambdaQueryWrapper<SchoolCalendarInfo>().between(SchoolCalendarInfo::getEndTime, endTime1, endTime2));
        Integer time=0;
        for (SchoolCalendarInfo calendarInfo : list) {
            Integer weeks = calendarInfo.getWeeks();
            time=weeks*7+time;
        }
        //创建返回对象
        StudentMeetDTO student=new StudentMeetDTO();
        student.setStudentName(studentInfo.getStudentName());
        student.setOnSchoolTime(time);
        return R.success(student);
    }

    /**
     * 获取院系信息
     * @param num
     * @return
     */
    @Override
    public R<InstituteInfoDTO> getInstitute(String num) {
        InstituteInfoDTO instituteInfoDTO=new InstituteInfoDTO();
        StudentInfo studentInfo = getOne(new LambdaQueryWrapper<StudentInfo>().eq(StudentInfo::getStudentNumber, num));
        //获取学号
        String studentNumber = studentInfo.getStudentNumber();

        //获取入学时间
        Date enrollmentTime = studentInfo.getEnrollmentTime();
        //班级编号
        String classNumber = studentInfo.getClassNumber();
        ClassInfo classInfo = classInfoService.getOne(new LambdaQueryWrapper<ClassInfo>().eq(ClassInfo::getClassNumber, classNumber));
        //获取班级
        String className = classInfo.getClassName();
        //获取专业编号
        String majorNumber = studentInfo.getMajorNumber();
        MajorInfo majorInfo = majorInfoService.getOne(new LambdaQueryWrapper<MajorInfo>()
                .eq(MajorInfo::getMajorNumber, majorNumber));
        //获取专业名称
        String majorName = majorInfo.getMajorName();
        //获取所属学院编号
        String departmentNumber = majorInfo.getDepartmentNumber();
        DepartmentInfo departmentInfo = departmentInfoService.getOne(new LambdaQueryWrapper<DepartmentInfo>().eq(DepartmentInfo::getDepartmentNumber, departmentNumber));
        //获取学院名称
        String departmentName = departmentInfo.getDepartmentName();

        //拼接院系
        String institute="";
        institute=departmentName+"-"+majorName+"-"+className;
        //封装数据
        instituteInfoDTO.setStudentNum(studentNumber);
        instituteInfoDTO.setRegisterTime(enrollmentTime);
        instituteInfoDTO.setDepartment(institute);
        return R.success(instituteInfoDTO);
    }

    /**
     * 获取课程信息
     * @param num
     * @return
     */
    @Override
    public R<CourseInfoDTO> getCourseInfo(String num) {
        StudentInfo studentInfo = getOne(new LambdaQueryWrapper<StudentInfo>().eq(StudentInfo::getStudentNumber, num));
        //获取班级的编号
        String classNumber = studentInfo.getClassNumber();
//
        //获取当前最新校历
        SchoolCalendarInfo last = calendarInfoService.getOne(new LambdaQueryWrapper<SchoolCalendarInfo>()
                .orderByDesc(SchoolCalendarInfo::getEndTime).last("limit 1"));
        //得到当前学期
        String term;
        String schoolYear = last.getSchoolYear();
        String semester = last.getSemester();
        int i = Integer.parseInt(semester);
        if (i == 1) {
            term = schoolYear + "学年第一学期";
        } else {
            term = schoolYear + "学年第二学期";
        }
        Integer id = last.getId();
        //获取班级从大一到现在的所有课表数量
        long count = scheduleInfoService.list(new LambdaQueryWrapper<ScheduleInfo>().eq(ScheduleInfo::getClassNumber, classNumber)).stream().count();
        //再根据当前的最新校历获取到当前学期的课表
        List<ScheduleInfo> list = scheduleInfoService.list(new LambdaQueryWrapper<ScheduleInfo>().eq(ScheduleInfo::getId, id).eq(ScheduleInfo::getClassNumber,classNumber));
        //最后再把当前学期的课程名收集到集合
        List<String> collect = list.stream().map((ScheduleInfo scheduleInfo) -> {
            String className = scheduleInfo.getCourseName();
            return className;
        }).collect(Collectors.toList());
        CourseInfoDTO dto = new CourseInfoDTO();
        dto.setAllCourseAmount((int) count);
        dto.setCurrentCourseAmount(collect.size());
        dto.setTermName(term);
        dto.setCurrentCourseNameList(collect);
        return R.success(dto);
    }

    /**
     *
     * @param num
     * @return
     */
    @Override
    public R<CreditDTO> getCredit(String num) {
        StudentInfo studentInfo = getOne(new LambdaQueryWrapper<StudentInfo>().eq(StudentInfo::getStudentNumber, num));

        //获取当前最新校历
        SchoolCalendarInfo last = calendarInfoService.getOne(new LambdaQueryWrapper<SchoolCalendarInfo>()
                .orderByDesc(SchoolCalendarInfo::getEndTime).last("limit 1"));
        Integer id = last.getId();
        //获取班级编号
        String classNumber = studentInfo.getClassNumber();
        //再根据当前的最新校历获取到当前学期的课表
        List<ScheduleInfo> list = scheduleInfoService.list(new LambdaQueryWrapper<ScheduleInfo>().eq(ScheduleInfo::getId, id).eq(ScheduleInfo::getClassNumber,classNumber));
        //学分
        Double credit=0.0;
        for (ScheduleInfo scheduleInfo : list) {
            String courseNumber = scheduleInfo.getCourseNumber();
            CourseInfo c = courseInfoService.getOne(new LambdaQueryWrapper<CourseInfo>().eq(CourseInfo::getCourseNumber, courseNumber));
            credit += c.getCredit();
        }
        CreditDTO creditDTO=new CreditDTO();
        creditDTO.setCredit(credit);
        return R.success(creditDTO);
    }
}

